问答:旋转窗口后的Python 2.7.3 + OpenCV 2.4不适合Image 您所在的位置:网站首页 opencv 图片旋转 问答:旋转窗口后的Python 2.7.3 + OpenCV 2.4不适合Image

问答:旋转窗口后的Python 2.7.3 + OpenCV 2.4不适合Image

2023-04-12 22:29| 来源: 网络整理| 查看: 265

我试图将图像旋转一定角度然后在窗口中显示.我的想法是旋转,然后在一个新的窗口中显示它,窗口的新宽度和高度从旧的宽度和高度计算:

new_width = x * cos angle + y * sin angle new_height = y * cos angle + x * sin angle

我期待结果如下所示:

在此输入图像描述

但事实证明结果如下:

在此输入图像描述

我的代码在这里:

#!/usr/bin/env python -tt #coding:utf-8 import sys import math import cv2 import numpy as np def rotateImage(image, angle):#parameter angle in degrees if len(image.shape) > 2:#check colorspace shape = image.shape[:2] else: shape = image.shape image_center = tuple(np.array(shape)/2)#rotation center radians = math.radians(angle) x, y = im.shape print 'x =',x print 'y =',y new_x = math.ceil(math.cos(radians)*x + math.sin(radians)*y) new_y = math.ceil(math.sin(radians)*x + math.cos(radians)*y) new_x = int(new_x) new_y = int(new_y) rot_mat = cv2.getRotationMatrix2D(image_center,angle,1.0) print 'rot_mat =', rot_mat result = cv2.warpAffine(image, rot_mat, shape, flags=cv2.INTER_LINEAR) return result, new_x, new_y def show_rotate(im, width, height): # width = width/2 # height = height/2 # win = cv2.cv.NamedWindow('ro_win',cv2.cv.CV_WINDOW_NORMAL) # cv2.cv.ResizeWindow('ro_win', width, height) win = cv2.namedWindow('ro_win') cv2.imshow('ro_win', im) if cv2.waitKey() == '\x1b': cv2.destroyWindow('ro_win') if __name__ == '__main__': try: im = cv2.imread(sys.argv[1],0) except: print '\n', "Can't open image, OpenCV or file missing." sys.exit() rot, width, height = rotateImage(im, 30.0) print width, height show_rotate(rot, width, height)

我的代码中肯定会有一些愚蠢的错误导致这个问题,但是我无法弄明白......而且我知道我的代码不够pythonic :( ...那就是......

谁能帮我?

最好,

bearzk



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有